package com.purong.purong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.purong.purong.R;
import com.purong.purong.bean.SmallLoanBean;
import com.purong.purong.util.CommonUtil;
import com.purong.purong.widget.RadiusImageView;
import com.yao.sdk.glide.GlideUtil;

import java.util.ArrayList;
import java.util.List;

import me.grantland.widget.AutofitTextView;

/**
 * Created by wenjiezeng on 2017/11/10.
 */
public class SmallLoanAdapter extends BaseAdapter {
    private List<SmallLoanBean> loanList = new ArrayList<>();
    private Context mContext;
    public SmallLoanAdapter(Context context, List<SmallLoanBean> list) {
        this.mContext = context;
        this.loanList=list;
    }

    @Override
    public int getCount() {
        return loanList.size();
    }

    @Override
    public Object getItem(int position) {
        return loanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.small_loan_item, null);
            mHolder = new ViewHolder(convertView);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        mHolder.name.setText(loanList.get(position).getSmall_product_name());
        mHolder.money.setText(CommonUtil.numConvertSubZero(Integer.valueOf(loanList.get(position).getMin_small_money()),2)+"~"+ CommonUtil.numConvertSubZero(Integer.valueOf(loanList.get(position).getMax_small_money()),2));
        mHolder.rate.setText("月利率"+ loanList.get(position).getRate());
        mHolder.lending.setText(loanList.get(position).getLending_day()+"放款");
        mHolder.period.setText("贷款期限"+loanList.get(position).getPeriod());
        GlideUtil.getInstance().load(mContext,loanList.get(position).getSmall_product_url(),mHolder.img,true);
        if (loanList.get(position).getStatus().equals("1")){
            mHolder.apply.setText("申请贷款");
            mHolder.apply.setTextColor(mContext.getResources().getColor(R.color.white));
            mHolder.apply.setBackgroundResource(R.drawable.gradient_darkblue_radius2);
        }else {
            mHolder.apply.setText("人数已满");
            mHolder.apply.setTextColor(mContext.getResources().getColor(R.color.white));
            mHolder.apply.setBackgroundResource(R.drawable.button_gray_radius2);
        }
        if (loanList.get(position).getTagging_title()!=null&&!loanList.get(position).getTagging_title().equals("")
                &&loanList.get(position).getTagging_content()!=null&&!loanList.get(position).getTagging_content().equals("")){
            mHolder.taggingLay.setVisibility(View.VISIBLE);
            mHolder.taggingCutLine.setVisibility(View.VISIBLE);
            mHolder.taggingTitle.setText(loanList.get(position).getTagging_title());
            mHolder.taggingContent.setText(loanList.get(position).getTagging_content());
        }else {
            mHolder.taggingLay.setVisibility(View.GONE);
            mHolder.taggingCutLine.setVisibility(View.GONE);
        }
        if (loanList.get(position).getSmall_tag()==null||loanList.get(position).getSmall_tag().size()==0){
            mHolder.tagLay.setVisibility(View.GONE);
        }else {
            mHolder.tagLay.setVisibility(View.VISIBLE);
            mHolder.tagOne.setVisibility(View.GONE);
            mHolder.tagTwo.setVisibility(View.GONE);
            mHolder.tagThree.setVisibility(View.GONE);
            for (int i = 0; i <loanList.get(position).getSmall_tag().size() ; i++) {
                if (i==0){
                    mHolder.tagOne.setVisibility(View.VISIBLE);
                    mHolder.tagOne.setText(loanList.get(position).getSmall_tag().get(0));
                }else if (i==1){
                    mHolder.tagTwo.setVisibility(View.VISIBLE);
                    mHolder.tagTwo.setText(loanList.get(position).getSmall_tag().get(1));
                }else if (i==2){
                    mHolder.tagThree.setVisibility(View.VISIBLE);
                    mHolder.tagThree.setText(loanList.get(position).getSmall_tag().get(2));
                }
            }
        }
        if (position!=loanList.size()-1){
            mHolder.cutLine.setVisibility(View.VISIBLE);
        }else {
            mHolder.cutLine.setVisibility(View.GONE);

        }
        return convertView;
    }
    public  class ViewHolder extends RecyclerView.ViewHolder {
        //绑定每个CircleItem的控件引用到ViewHolder
        private RadiusImageView img;
        private TextView name,rate,lending,period,apply, taggingTitle, taggingContent,tagOne,tagTwo,tagThree;
        private AutofitTextView money;
        private View cutLine, taggingCutLine;
        private LinearLayout taggingLay,tagLay;
        public ViewHolder(View v) {
            super(v);
            img=(RadiusImageView)v.findViewById(R.id.img);
            name=(TextView)v.findViewById(R.id.name);
            money=(AutofitTextView)v.findViewById(R.id.money);
            rate=(TextView)v.findViewById(R.id.rate);
            lending=(TextView)v.findViewById(R.id.lending);
            period=(TextView)v.findViewById(R.id.period);
            taggingTitle =(TextView)v.findViewById(R.id.tagging_title);
            taggingContent =(TextView)v.findViewById(R.id.tagging_content);
            apply=(TextView)v.findViewById(R.id.apply);
            cutLine=(View)v.findViewById(R.id.cut_line);
            taggingCutLine =(View)v.findViewById(R.id.tagging_cut_line);
            taggingLay =(LinearLayout)v.findViewById(R.id.taggling_lay);
            tagLay=(LinearLayout)v.findViewById(R.id.tag_lay);
            tagOne=(TextView)v.findViewById(R.id.tag_one);
            tagTwo=(TextView)v.findViewById(R.id.tag_two);
            tagThree=(TextView)v.findViewById(R.id.tag_three);

        }
    }

}
